Adventures In Raspberry Pi by Carrie Anne Philbin
Author:Carrie Anne Philbin [Philbin, Carrie Anne]
Language: eng
Format: azw, pdf
Publisher: Wiley
Published: 2013-12-03T00:00:00+00:00
Using a while Loop
So far, the player has not been required to input specific answers in order for the game to move on. If the player does not input anything at all, the game simply stalls; and if the player types an unrecognized answer, the game says, âYou think for a whileâ. You want the player to input one of the responses that you have defined, left or right, to move on to the next location. You can ensure she inputs one of the desired responses by adding a while loop to your code. This will loop the user input question until the player types in a response that you were looking forâleft or rightâto move on. For example:
# Loop until we get a recognised response
while True:
direction1 = input(âDo you want to go left or right? â)
direction1 = direction1.lower()
if direction1 == âleftâ:
print(âYou walk to the cave and notice there is an
opening.â)
break # leave the loop
elif direction1 == ârightâ:
print(âYou walk to the beach but remember you do not
have any swimwear.â)
break # leave the loop
else:
print(âYou think for a whileâ)
In this code, shown also in Figure 5-11, you can see in bold text the Python words while True: added before the user input question, and break added within the conditionals for left and right. The while True: condition will loop the question over and over until the player enters either left or right so that the game does not end if the player types anything else.
Download
Adventures In Raspberry Pi by Carrie Anne Philbin.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Lion King by Disney Book Group(864)
Figaro and Rumba and the Cool Cats by Anna Fienberg(842)
The Age of Dinosaurs by Steve Brusatte(775)
Tales from the Ant World by Edward O. Wilson(758)
The DNA Book by DK(719)
The Book of Hope by Jane Goodall(719)
The Rainforest Book by Charlotte Milner(690)
Time To Go by Time To Go (epub)(645)
How to Draw Everything by Peter Gray(640)
Small Favors by Erin A. Craig(639)
A Most Remarkable Creature by Jonathan Meiburg(629)
Dirty Beasts by Roald Dahl & Quentin Blake(618)
Beginner's Bet by Fiona Riley(587)
Don Cherry's Hockey Stories and Stuff by Don Cherry(582)
Fox 8: A Story by George Saunders(577)
The Valley and the Flood by Rebecca Mahoney(576)
Ants by M. V. Brian(555)
Paper Heart by Cat Patrick(535)
Rescue on the River by Marianne Hering & Sheila Seifert(530)
